<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧边栏 -->
      <el-aside :width="asideWidth">
        <!-- 顶部点击折叠区域 -->
        <div class="top-bar" @click="toggle">|||</div>
        <!-- 左侧菜单区域 -->
        <el-menu
          unique-opened
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id+''" v-for="(item,index) in menuList" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="['iconfont',icons[index]]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      icons: [
        "icon-user",
        "icon-tijikongjian",
        "icon-shangpin",
        "icon-danju",
        "icon-baobiao"
      ],
      isCollapse: false
    };
  },
  created() {
    this.getMenuList();
  },
  computed: {
    asideWidth() {
      return this.isCollapse ? "64px" : "200px";
    },
    activePath() {
      // console.log(this);
      // console.log(this.$route.path);
      return this.$route.path;
      // return
    }
  },
  methods: {
    //登录
    async logout() {
      const confirmRes = await this.$confirm("确认是否退出?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      console.log(confirmRes);
      if (confirmRes == "confirm") {
        window.sessionStorage.clear();
        this.$router.push("/login");
      }
    },
    // 获取菜单列表数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    // 折叠方法
    toggle() {
      this.isCollapse = !this.isCollapse;
    }
  }
};
</script>
<style lang="less" scoped>
// 让最大容器撑满屏幕
.home-container {
  height: 100%;
}
// 给三个区域设置背景色
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: none;
}
.el-menu-item {
  font-size: 12px;
}
/deep/.el-submenu__title {
  font-size: 12px;
}
.top-bar {
  // height: 24px;
  line-height: 24px;
  color: white;
  font-size: 12px;
  background-color: #4a5064;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2em;
}
</style>

